package xadrez;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

public class GeradorDeTabuleiro extends JPanel implements MouseListener{
	/**
	 * 
	 */
	private int resolucao;
	private Point p;
	private Tabuleiro tabuleiro;
	private Sprite spritePreto[];
	private Sprite spriteBranco[];
	private Sprite pecaSelecionada;
	private PlanoDeFundo fundo;
	private boolean trava;
	private boolean fim;
	
	public GeradorDeTabuleiro(int resolucao) {
		super();		
		this.resolucao = resolucao;
		fundo = new PlanoDeFundo(resolucao);
		fim = false;
		trava = false;
		p = null;
		tabuleiro = new Tabuleiro();
		tabuleiro.limpaTabuleiro();
		spritePreto = new Sprite[6];
		spriteBranco = new Sprite[7];
		spritePreto[0] = new Sprite('b',"preto",resolucao);
		spritePreto[1] = new Sprite('c',"preto",resolucao);
		spritePreto[2] = new Sprite('k',"preto",resolucao);
		spritePreto[3] = new Sprite('p',"preto",resolucao);
		spritePreto[4] = new Sprite('q',"preto",resolucao);
		spritePreto[5] = new Sprite('t',"preto",resolucao);		
		spriteBranco[0] = new Sprite('b',"branco",resolucao);
		spriteBranco[1] = new Sprite('c',"branco",resolucao);
		spriteBranco[2] = new Sprite('k',"branco",resolucao);
		spriteBranco[3] = new Sprite('p',"branco",resolucao);
		spriteBranco[4] = new Sprite('q',"branco",resolucao);
		spriteBranco[5] = new Sprite('t',"branco",resolucao);
		spriteBranco[6] = new Sprite('x',"null",resolucao);                
                              
                this.addMouseListener(this);    	
                repaint();		
	}
	
	
	@Override
	public void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponents(g);                                 
		fundo.Desenha(g, tabuleiro); 
                g.setColor(Color.WHITE);
                g.fillRect(0, resolucao, resolucao, resolucao + (resolucao/4));
                g.setColor(Color.gray);
                g.fillRect((resolucao/8)*7,resolucao + (resolucao/8), resolucao, resolucao);
		for (int i = 0; i < 6; i++) {
			spritePreto[i].paint(g, i*(resolucao/8), resolucao);
			spriteBranco[i].paint(g, i*(resolucao/8), resolucao+(resolucao/8));
		}                
	}
	
	public Tabuleiro criaTabuleiro() {
                repaint();
		while (!fim) {	                                               
			while ((p == null)&&(!fim)) {
				try {
					Thread.sleep(300);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
			if (!fim) {
				adicionaPeca();
				trava = false;
				p = null;
			}			
			repaint();
		}
		return tabuleiro;
	}
	
	private void adicionaPeca() {
		int x = p.x/(resolucao/8);
		int y = p.y/(resolucao/8);
		if (p.y < resolucao) {
			Peca peca = new Peca(pecaSelecionada.getTipo(),x,y,pecaSelecionada.getCor());
			tabuleiro.SetPeca(peca, x, y);
		}
		
	}
	
	private void selecionaPeca(Point p) {
		if (p.y > resolucao) {
			if (p.x > (resolucao/8)*6) {
				pecaSelecionada = spriteBranco[6];
			}else {
				if (p.y < (resolucao + resolucao/8)) {
					pecaSelecionada = spritePreto[p.x/(resolucao/8)];
				}else {
					pecaSelecionada = spriteBranco[p.x/(resolucao/8)];
				}
			}
                        if ((p.y > resolucao + (resolucao/8)) && (p.x > (resolucao/8)*7))
                                fim = true;
		}
	}
	
	
	
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
		
	}
	
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		if (!trava) {
			p = e.getPoint();
			trava = true;
			selecionaPeca(p);			
		}
	}
	
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
        
        	
	
}
